package com.resou.reader.discovery;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.MainActivity;
import com.resou.reader.R;
import com.resou.reader.api.ApiConstant;
import com.resou.reader.api.entry.ActivityBean;
import com.resou.reader.api.entry.DisCoveryBean;
import com.resou.reader.assist.WebActivity;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.bookstore.category.AllCategoryActivity;
import com.resou.reader.bookstore.freezone.FreezoneActivity;
import com.resou.reader.bookstore.rank.RankMainActivity;
import com.resou.reader.discovery.adapter.DisCoveryAdapter;
import com.resou.reader.discovery.choicenes.ChoiceneBookActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.vipcenter.MonthlyVipActivity;
import com.resou.reader.search.search2.Search2Activity;
import com.resou.reader.utils.StatusBarUtil;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.ViewSizeUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeDiscoveryFragment extends ResouBaseFragment<HomeDiscoveryPresenter> implements IDiscoveryContract.IDiscoveryView, BaseQuickAdapter.OnItemChildClickListener {

    /**
     * 收费版
     */
    private final int TAGCHOICENES = 5;//精品书单
    private final int TAGTYPE = 6;//分类
    private final int TAGRANK = 7;  //排行榜
    private final int TAGGIFT = 8; //礼品
    private final int TAGFREE = 9;//免费
    private final int TAGMONTHLY = 20;//包月VIP

    private static final String TAG = "HomeDiscoveryFragment";
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.lt_search)
    LinearLayout ltSearch;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.rl_discovery_search)
    RelativeLayout rl_discovery_search;
    @BindView(R.id.v_state_bar)
    View v_state_bar;
    DisCoveryAdapter disCoveryAdapter;
    ActivityBean activityBean = null;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_discovery;
    }

    @Override
    protected void initView() {

        // 和顶部保持一定的距离
        int height = ViewSizeUtils.getStatusBarHeight(mActivity.getResources()) + ((MainActivity) mActivity).getState();
//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rl_discovery_search.getLayoutParams();
//        layoutParams.setMargins(0, height - 10, 0, 0);
//        rl_discovery_search.setLayoutParams(layoutParams);
        rl_discovery_search.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        v_state_bar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        ViewGroup.LayoutParams lp = v_state_bar.getLayoutParams();
        lp.height = height;
        v_state_bar.setLayoutParams(lp);

        ivBack.setVisibility(View.GONE);
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setPrimaryColors(getResources().getColor(R.color.theme_color));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        disCoveryAdapter = new DisCoveryAdapter(R.layout.adapter_discovery, null);
        mSmartRefreshLayout.setEnableLoadMore(false);
        mRecyclerView.setAdapter(disCoveryAdapter);
        disCoveryAdapter.setOnItemChildClickListener(this);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.quearData();
            }
        });
        mSmartRefreshLayout.setEnableOverScrollBounce(true);
    }

    @Override
    protected void retryRefreshData() {
        showLoading();
        mPresenter.quearData();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new HomeDiscoveryPresenter(this);
    }


    @Override
    public void showContent() {
        mSmartRefreshLayout.finishRefresh();
        dissLoading();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {
        dissLoading();
        mSmartRefreshLayout.finishRefresh();
    }

    @Override
    public void initData() {
        showLoading();
        mPresenter.quearData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            System.out.println("class:" + this.getClass().getName() + "\thidden:" + hidden);
            mPresenter.quearData();
            StatusBarUtil.setStatusBarColor(getActivity(), getResources().getColor(R.color.colorPrimaryDark));
        } else {
            dissLoading();
        }
    }


    @OnClick({R.id.lt_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            /**
             * 搜索
             */
            case R.id.lt_search:
                Search2Activity.start(getContext());
                break;
        }
    }

    @Override
    public void showData(List<DisCoveryBean> list) {
        disCoveryAdapter.setNewData(list);
        if (UserInstance.isLogin()) {
            mPresenter.quearActivityInfo();
        }
    }

    @Override
    public void showAcitivty(ActivityBean activityBean) {
        disCoveryAdapter.updataActivityInfo(activityBean);
        this.activityBean = activityBean;
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        List<DisCoveryBean> list = adapter.getData();
        switch (list.get(position).getId()) {
            /**
             * 精品书单
             */
            case TAGCHOICENES:
                ChoiceneBookActivity.start(getContext());
                break;
//            /**
//             * VIP免广告
//             */
//            case TAGVIP:
//                VipCenter.start(getContext());
//                break;
            /**
             * 分类
             */
            case TAGTYPE:
                AllCategoryActivity.startActivity(getContext());
                break;
            /**
             * 包月VIP
             */
            case TAGMONTHLY:
                MonthlyVipActivity.start(getContext());
                break;

            /**
             * 排行榜
             */
            case TAGRANK:
                RankMainActivity.startActivity(getContext());
                break;
            /**
             * 礼物
             */
            case TAGGIFT:
                WebActivity.startActivity(getActivity(), ApiConstant.VIPGIFT + UserInstance.getToken(), "活动有礼");
                break;
            /**
             * 免费书籍
             */
            case TAGFREE:
                FreezoneActivity.start(getContext());
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.quearActivityInfo();
    }
}
